//监听事件的开始
layui.use(['upload', 'form'], function () {


    var form = layui.form, upload = layui.upload,
        $ = layui.jquery;

    //  如果状态是 质检完成 不能更改页面属性
    var qualityStatus = $("[name=qualityStatus]").val();
    var checkItemFlag = $("[name=checkItemFlag]").val();

    if (qualityStatus == '3') {
        form.render('select');
    }

    // 检验方案change事件

    form.on('select(interest)', function (data) {
        var checkScenarioName = data.value;
        var sampleKey = getInspectionPlanMgt(checkScenarioName);
        console.log(sampleKey);
        //根据抽样水平编码 重载AQL页面
        testReload2(sampleKey, checkScenarioName);
        //重载检验项目表
        testReload3(checkScenarioName);
    });

    // 判定方式change事件
    form.on('select(decideWay)', function (data) {

        if (qualityStatus != '3') {
            var noOk = data.elem[data.elem.selectedIndex].text;
            //更新 将检验明细中的拒收数量、验退数量清空 入库数量 = 待检数量
            $.get("/3nod_web/iqcDetectionDetail/cleanData", "sku=" + sku + "&receiptKey=" + receiptKey + "&noOk=" + noOk, function (data) {
                detectionDetailReload();
                //更新表头的入库数、拒收数量
                $.get("/3nod_web/iqcDetectionDetail/fill", "sku=" + sku + "&receiptKey=" + receiptKey, function (data) {
                    $("[name=rejectionQuantity]").val(data.rejectionQuantity);
                    $("[name=inventoryQuantity]").val(data.inventoryQuantity);
                    $("[name=checkBackQuantity]").val(data.checkBackQuantity);
                });
            });
        }
    });
    var submitFlag = false;//防重复提交标志
    var saveFlag = false;//防重复提交标志
    var flag = false;//文件上传标识

    // form.on('select(decideWay)', function(data){
    //     if(saveFlag){
    //         if(data.value == 2){
    //             $('#specialMining').removeClass('layui-hide');
    //             $('#upload').removeClass('layui-hide');
    //             $('#uploadFile').removeClass('layui-btn-disabled');
    //         }else {
    //             $('#specialMining').addClass('layui-hide');
    //             $('#upload').addClass('layui-hide');
    //         }
    //         $("#fileLoad").val("");
    //         flag = false;
    //     }
    // });

    $('#uploadFile').on('click', function () {
        if (flag) {
            layer.confirm('文件已上传,是否再次上传？', {
                btn: ['确定', '取消'] //可以无限个按钮
                , btn2: function (index, layero) {
                    layer.close(index);
                    return false;
                }
            }, function (index, layero) {
                layer.close(index);
                // 模拟点击上传按钮
                $('#uploadFile1').click();
            });
        } else {
            // 模拟点击上传按钮
            $('#uploadFile1').click();
        }
    });

    //指定允许上传的文件类型
    upload.render({
        elem: '#uploadFile1'
        , url: '/3nod_web/file/uploadFile' //改成您自己的上传接口
        , accept: 'file' //普通文件
        , done: function (res) {
            if (res.code == "S") {
                layer.msg('上传成功');
                flag = true;
                $("#fileName").val(res.fileName);
            } else {
                layer.msg(res.msg);
            }
            return;
        }
    });

    // 保存按钮
    form.on('submit(save)', function (data) {
        if (qualityStatus == '3') {
            checkItemSave();
            layer.alert("保存成功");
        }else{
            if (!submitFlag) {
                data.field.decideWay = $("[name=decideWay]").val();
                submitFill();
                $.ajax({
                    url: '/3nod_web/iqcDetection/update',
                    type: 'POST',
                    contentType: 'application/json; charset=UTF-8',
                    async: false,
                    dataType: 'json',
                    data: JSON.stringify(data.field),
                    success: function (data) {
                        if (data == '请求超时，请点击左侧菜单，重新载入!') {
                            layer.alert(data);
                            return false;
                        }
                        if (data.code == 500) {
                            layer.alert("保存失败");
                            alert(data.msg)
                        } else {
                            layer.alert("保存成功");
                            detectionDetailReload();
                            saveFlag = true;
                        }
                    }
                })
            } else {
                layer.alert("请不要重复提交!");
            }
        }
    });


    // 提交按钮
    form.on('submit(submit)', function (data) {
        data.field.decideWay = $("[name=decideWay]").val();
        console.log($("[name=decideWay]").val());
        debugger;
        //已质检、未填写检验项目
        if (qualityStatus == '3' && checkItemFlag != 'Y') {
            checkItemOver(data);
        } else {
            if (!saveFlag) {
                layer.alert("请保存后再提交");
                return;
            }
            if (!submitFlag) {
                // 已质检、未填检验项目的数据


                submitFill();
                //判定方式
                var decideWay = $("#decideWay").val();
                //不合格数量
                var unqualifiedQuantity = $("[name=unqualifiedQuantity]").val();
                // var decideWay = $("[name=decideWay]").val();
                //机型
                var type = $("#type").val();
                //检验结果
                var checkResult = $("[name=checkResult]").val();
                //不良原因
                var culprit = $("[name=culprit]").val();
                //检验方案
                var scheme = $("#scheme").val();

                if (decideWay == " ") {
                    layer.alert("判定方式不能为空");
                } else if (type == "") {
                    layer.alert("机型不能为空");
                } else if (scheme == "") {
                    layer.alert("检验方案不能为空");
                } else if (checkResult == "不合格" && culprit == "") {
                    layer.alert("由于检测结果不合格，不良原因必填");
                } else if (unqualifiedQuantity == null || unqualifiedQuantity == '') {
                    layer.alert("不合格数量不能为空");
                } else if (unqualifiedQuantity == 0 && checkResult == '不合格') {
                    layer.alert("不合格数量必填");
                } else {
                    //保存的时候判断检验结果
                    $.get("/3nod_web/iqcCheckItemDetails/getCheckResult", "sku=" + sku + "&receiptKey" + receiptKey, function (data) {
                        //改变检验结果的值
                        $("[name=checkResult]").val(data);
                    });

                    $.ajax({
                        url: '/3nod_web/iqcDetection/submit',
                        type: 'POST',
                        contentType: 'application/json; charset=UTF-8',
                        async: false,
                        dataType: 'json',
                        data: JSON.stringify(data.field),
                        success: function (data) {
                            if (data == '请求超时，请点击左侧菜单，重新载入!') {
                                layer.alert(data);
                                return false;
                            }
                            if (data.code == 500) {
                                //layer.alert("提交失败");
                                alert(data.msg)
                            } else {
                                alert("提交成功");
                                detectionDetailReload();
                                submitFlag = true;
                                var library = $("#library").val();
                                var users = $("#users").val();
                                $.get("/3nod_web/iqcDetection/setReturnFlag?flag=1");
                                location.href = '/3nod_web/iqc/iqcManager?library=' + library + "&users=" + users;

                            }

                            // location.href='/3nod_web/iqc/iqcManager';
                        }
                    });
                }

            } else {
                layer.alert("请不要重复提交!");
            }
        }

    });

});